<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<html>
 <head>
     <style type="text/css" >
         #process{
             display: none; position: absolute; top: 0; bottom: 0; left: 0; right: 0; background: #000; opacity: 0.7; z-index: 999; cursor: wait;
         }
         .pro{
             text-align: center; font-size: 38px; top: 36%;position: relative;
         }
         #process_text {
             color: white; text-align: center; font-size: 18px; top: 42%;position: relative;
         }
         #rulePieceImport{
             margin-bottom: 30px;
         }
         .btnleft{
             float: left; height: 39px; line-height: 29px; margin-right: 5px;
         }
     </style>
 </head>
<body>
    <div class="box" ng-app="RuleApp" ng-controller="RuleCtrl">
        <div id="process" >
            <div class="pro"></div>
            <div id="process_text"></div>
        </div>
        <div class="row" id="rulePieceImport">
            <a class="btn btn-sm btn-primary btnleft" href="javascript:;" ng-click="exportPieceTpl()" target="_blank" >下载按件计算规则模板</a>
            <ui-upload  url="rule/newrules/piece/import" success="uploadPieceSuccess" show="showPieceProcess" hide="hidePieceProcess" ></ui-upload>
        </div>
        <div class="row" id="ruleImport">
            <a class="btn btn-sm btn-primary btnleft" href="javascript:;" ng-click="exportTpl()" target="_blank" >下载按重计算规则模板</a>
            <ui-upload  url="rule/newrules/import" success="uploadSuccess" show="showProcess" hide="hideProcess" ></ui-upload>
        </div>
    </div>
    <script type="text/javascript" >
        var ruleApp = angular.module('RuleApp',['ui.table']);
        ruleApp.controller('RuleCtrl', function($scope){
            $scope.uploadSuccess=function (datas) {
                tipMsg("下载按重验证失败记录",$ruleImport,datas);
            }
            $scope.exportTpl = function(){
                exports('rule/newrules/exporttemplate');
            }
            $scope.showProcess = function(){
                processShow({"type":"weightRuleProcess"});
            }
            $scope.showPieceProcess = function(){
                processShow({"type":"pieceRuleProcess"});
            }
            $scope.hideProcess = function(){
                processHide();
            }
            $scope.hidePieceProcess = function(){
                processHide();
            }
            $scope.uploadPieceSuccess=function (datas) {
                tipMsg("下载按件验证失败记录",$rulePieceImport,datas);
            }
            $scope.exportPieceTpl = function(){
                exports('rule/newrules/piece/exporttemplate');
            }
        });
        var timeid,pro,proDiv, $process_text,$process, $ruleImport,$rulePieceImport,$body;
        var proDiv = document.getElementsByClassName('pro')[0];
        var proConfig ={
            width : 555,
            height: 30,
            bgColor : "#1b1c1d",
            proColor : "#068043",
            fontColor : "#FFFFFF",
            val : 0,
            text:"#*val*#%",
            showPresent : true
        };
        var proRequest = function(param,callback){
            $.ajax({
                url: "rule/newrules/import/process",
                type: 'POST',
                data: param,
                async: false,
                cache: false,
                dataType:"json",
                success: function(data) {
                    data = data? eval("("+data+")"):"";
                    callback && callback(data);
                },
                error:function(jhr, status, error){
                    console.log(error);
                }
            });
        };
        var processShow = function(param){
            $body.css("overflow","hidden");
            pro = new progress(proConfig);
            proDiv.appendChild(pro.getBody());
            timeid = window.setInterval(function(){
                proRequest(param,function(data){
                    if(data == "end"){
                        $process.hide();
                        window.clearInterval(timeid);
                    } else if(data.val){
                        $process.show();
                        pro.update(data.val);
                        $process_text.html(data.msg);
                    }
                });
            },500);
        };
        var processHide = function(){
            $process.hide();
            window.clearInterval(timeid);
            proDiv.removeChild(pro.getBody());
        };
        var tipMsg = function(text,el,datas){
            if (datas.data["flag"] ==2){
                toastr.success(datas.data["desc"]);
            } else {
                toastr.error(datas.data["desc"])
                el.find(".upload-result").remove();
                el.append('<a  class="btn btn-sm btn-success upload-result" href="rule/newrules/down?fileKey='+datas.data["fileKey"]+'" target="_blank" >'+text+'</a>').css("margin-bottom","30px");
            }
        };

        var exports = function(url){
            var $exportForm = $("<form>").css("display", "none")
                .attr('target', 'targetFrame')
                .attr('method', 'POST')
                .attr('action', url)
                .appendTo($("body")).submit();
            $exportForm.remove();
        };
        $(function(){
            $process_text = $("#process_text");
            $process = $("#process");
            $rulePieceImport = $("#rulePieceImport");
            $ruleImport = $("#ruleImport");
            $body = $("body");
        });
    </script>
</body>
</html>


